package com.easyedu.dean.controller;

import com.alibaba.excel.EasyExcel;
import com.easyedu.core.util.R;
import com.easyedu.dean.entity.excel.Student2Excel;
import com.easyedu.dean.service.StudentService;
import com.easyedu.dean.util.excel.SaveStuData;
import java.io.IOException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author lmr
 **/
@RestController
@RequestMapping("/dean/file/upload")
public class FileUploadController {

     private final StudentService studentService;

     public FileUploadController(StudentService studentService) {
          this.studentService = studentService;
     }

     /**
      * 上传学生信息
      * @param file 文件内容
      * @return 成功失败
      */
     @PostMapping("/create/student")
     public R createStudent(@RequestParam(value = "file", required = false) MultipartFile file) {
          try {
               EasyExcel.read(file.getInputStream(), Student2Excel.class, new SaveStuData(studentService)).sheet()
                       .doRead();
          } catch (Exception e) {
               return R.error().message("上传失败");
          }
          return R.ok().message("上传成功");
     }

}
